package com.zsc.wxm.ysh.anjo.house.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.wxm.ysh.anjo.house.cache.ImagesCacheService;
import com.zsc.wxm.ysh.anjo.house.feign.OssService;
import com.zsc.wxm.ysh.anjo.house.mapper.ImagesMapper;
import com.zsc.wxm.ysh.anjo.house.model.Images;
import com.zsc.wxm.ysh.anjo.house.service.ImagesService;
import com.zsc.wxm.ysh.anjo.result.CodeEnum;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.exception.AnjoException;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.vo.ImagesVO;
import com.zsc.wxm.ysh.anjo.service.base.utils.BeanCopyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author playmaker
 * @since 2021-01-04
 */
@Service
public class ImagesServiceImpl extends ServiceImpl<ImagesMapper, Images> implements ImagesService {

    @Resource
    private OssService ossService;

    @Resource
    private ImagesCacheService imagesCacheService;

    @Override
    public void upload(Long resourcesId, MultipartFile[] files) {
        if (files != null && files.length > 0) {
            //先清空之前的图片
            deleteImage(resourcesId);
            List<Images> list = new ArrayList<>();
            for (MultipartFile file : files) {
                Images images = new Images();
                images.setHouseResourcesId(resourcesId);
                Result result = ossService.upload(file);
                if (!result.getSuccess()) {
                    throw new AnjoException(CodeEnum.FILE_UPLOAD_ERROR);
                }
                images.setImageUrl((String) result.getData().get("url"));
                list.add(images);
            }
            this.saveBatch(list);
        }
        //清除缓存
        imagesCacheService.removeListByResourcesIdCache(resourcesId);
    }

    @Override
    public void deleteImage(Long resourcesId) {
        LambdaQueryWrapper<Images> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Images::getHouseResourcesId, resourcesId);
        List<Images> list = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            this.remove(queryWrapper);
            list.forEach(x -> ossService.remove(x.getImageUrl()));
        }
        //清除缓存
        imagesCacheService.removeListByResourcesIdCache(resourcesId);
    }

    @Override
    public List<ImagesVO> list(Long resourceId) {
        List<Images> list = imagesCacheService.listByResourcesIdCache(resourceId);
        return BeanCopyUtil.copyListProperties(list, ImagesVO::new);
    }

    @Override
    public List<ImagesVO> listFirstImage(List<Long> resourceId) {
        List<ImagesVO> vos = new ArrayList<>();
        for (Long id : resourceId) {
            List<Images> list = imagesCacheService.listByResourcesIdCache(id);
            ImagesVO imagesVo = new ImagesVO();
            BeanUtils.copyProperties(list.get(0), imagesVo);
            vos.add(imagesVo);
        }
        return vos;
    }
}
